import { useEffect } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import useGetUserInfo from './useGetUserInfo';
import {
  isLoginOrRegisterPath,
  isNoNeedUserInfoPath,
  MANAGE_LIST_PATH,
  LOGIN_PATH,
} from '../router/index';

const useNavPage = (waitingUserData: boolean) => {
  const { username } = useGetUserInfo();
  const { pathname } = useLocation();
  const nav = useNavigate();
  useEffect(() => {
    if (waitingUserData) return;
    if (username) {
      if (isLoginOrRegisterPath(pathname)) nav(MANAGE_LIST_PATH);
      return;
    }
    if (!isNoNeedUserInfoPath(pathname)) nav(LOGIN_PATH);
  }, [waitingUserData, username, pathname, nav]);
};

export default useNavPage;
